<!doctype html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8" >
    <meta http-equiv="X-UA-Compatible" content="IE=11,IE=10,IE=9,IE=8" >
    <meta name="baidu-site-verification" content="dIcXMeY8Ya" />
    
    <title>Spring入门 | 即将拥有人鱼线丶</title>
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=0" >
    <meta name="keywords" content="FutaoSmile, 前端, Web, 付韬, 个人博客" >
    <meta name="description" content="付韬FutaoSmile" >

    
    <link rel="alternative" href="/atom.xml" title="即将拥有人鱼线丶" type="application/atom+xml" >
    
    
    <link rel="shortcut icon" href="/coder957/coder957/favicon.ico" >
    
    <link rel="stylesheet" href="/coder957/css/style.css">
    <!--[if lt IE 9]>
    <script src="/coder957/js/html5.js"></script>
    <![endif]-->
    
<script>
    var _hmt = _hmt || [];
    (function() {
        var hm = document.createElement("script");
        hm.src = "//hm.baidu.com/hm.js?fd459238242776d173cdc64918fb32f2";
        var s = document.getElementsByTagName("script")[0];
        s.parentNode.insertBefore(hm, s);
    })();
</script>


</head>

<body class="home">
    <!--[if lt IE 9]>
    <div class="browsehappy">
        当前网页 <strong>不支持</strong>
        你正在使用的浏览器. 为了正常的访问, 请 <a href="http://browsehappy.com/">升级你的浏览器</a>.
    </div>
    <![endif]-->

    <!-- 博客头部 -->
    <header class="header">
    <section class="container header-main">
        <div class="logo">
            <a href="/coder957/">
                <div class="cover">
                    <span class="name">即将拥有人鱼线丶</span>
                    <span class="description">FutaoSmile</span>
                </div>
            </a>
        </div>
        <div class="dropnav icon-paragraph-justify" id="JELON__btnDropNav"></div>
        <ul class="menu hidden" id="JELON__menu">
            
                <li rel="/coder957/2017/09/29/Spring入门/index.html" class="item ">
                    <a href="/coder957/" title="首页" class="icon-home">&nbsp;首页</a>
                </li>
                
                <li rel="/coder957/2017/09/29/Spring入门/index.html" class="item ">
                    <a href="/coder957/lab/" title="实验室" class="icon-lab">&nbsp;实验室</a>
                </li>
                
                <li rel="/coder957/2017/09/29/Spring入门/index.html" class="item ">
                    <a href="/coder957/about/" title="关于" class="icon-about">&nbsp;关于</a>
                </li>
                
                <li rel="/coder957/2017/09/29/Spring入门/index.html" class="item ">
                    <a href="/coder957/comment/" title="留言" class="icon-comment">&nbsp;留言</a>
                </li>
                
        </ul>
        <div class="profile clearfix">
            <div class="feeds fl">
                
                    
                        <p class="links">
                            
                                <a href="https://github.com/FutaoSmile" target="_blank">
                                    Github
                                </a>
                                |
                                    
                                        
                                <a href="http://www.jianshu.com/u/9e2e579df7dd" target="_blank">
                                    简书
                                </a>
                                
                                        
                        </p>
                        <p class="sns">
                            
                                <a href="http://weibo.com/u/2379076265?refer_flag=1001030001_&amp;nick=FutaoSmile%E4%B8%B6" class="sinaweibo" target="_blank"><b>■</b> 新浪微博</a>
                                
                                <a href="https://gitee.com/FutaoSmile" class="qqweibo" target="_blank"><b>■</b> 码云</a>
                                
                                    <!-- <a href="javascript: void(0);" class="wechat">
                        <b>■</b>
                        公众号
                        <span class="popover">
                            <img src="/coder957/img/wechat_mp.jpg" width="120" height="120" alt="我的微信订阅号">
                            <i class="arrow"></i>
                        </span>
                    </a> -->
                        </p>
                        
            </div>
            <div class="avatar fr">
                <img src="/coder957/img/head.png" alt="avatar" title="Jelon">
            </div>
        </div>
    </section>
</header>

    <!-- 博客正文 -->
    <div class="container body clearfix">
        <section class="content">
            <div class="content-main widget">
                <!-- 文章页 -->
<!-- 文章 -->
<article class="post article">
    <header class="text-center">
        <h3 class="post-title"><span>Spring入门</span></h3>
    </header>
    <p class="post-meta text-center">
        FutaoSmile 发表于
            <time datetime="2017-09-29T12:18:52.454Z">2017-09-29</time>
    </p>
    <div class="post-content">
        <blockquote>
<p>什么是Spring<br>Spring是一个容器，管理着整个应用程序中所有的bean的生命周期和依赖关系，从而降低耦合度，具体分为下面两种情况。</p>
</blockquote>
<a id="more"></a>
<p>spring的出现就是为了解耦合，充当一个管家的角色。<br>Spring把代码根据功能划分：</p>
<ol>
<li>主业务逻辑代码<ol>
<li>代码逻辑联系紧密，耦合度高，复用性低。</li>
<li>降低主业务代码之间的耦合度 =&gt;&gt; 控制反转 IOC</li>
</ol>
</li>
<li>系统级业务逻辑（交叉业务逻辑）<ol>
<li>功能相对独立，没有具体的业务场景，主要是提供系统级服务，如日志，安全，事务等，复用性很强。</li>
<li>降低主业务逻辑和系统级服务之间的耦合度 =&gt;&gt; 面向切面编程 AOP</li>
</ol>
</li>
</ol>
<p><img src="http://upload-images.jianshu.io/upload_images/1846623-d521cf1c3ee14d46.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="Spring体系结构"></p>
<h2 id="一，控制反转-IOC-Inversion-of-Control"><a href="#一，控制反转-IOC-Inversion-of-Control" class="headerlink" title="一，控制反转 IOC Inversion of Control"></a>一，控制反转 IOC Inversion of Control</h2><p>是一种思想，一个概念<br>IOC实现的两种方式</p>
<pre><code>1. 依赖注入 DI Dependency Injection 
2. 依赖查找 DL Dependency Lookup 
</code></pre><h3 id="创建第一个Spring项目"><a href="#创建第一个Spring项目" class="headerlink" title="创建第一个Spring项目"></a>创建第一个Spring项目</h3><p>新建Maven工程<br>POM文件<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div><div class="line">51</div><div class="line">52</div><div class="line">53</div><div class="line">54</div><div class="line">55</div><div class="line">56</div><div class="line">57</div><div class="line">58</div><div class="line">59</div><div class="line">60</div><div class="line">61</div><div class="line">62</div><div class="line">63</div><div class="line">64</div><div class="line">65</div><div class="line">66</div><div class="line">67</div><div class="line">68</div><div class="line">69</div><div class="line">70</div><div class="line">71</div><div class="line">72</div><div class="line">73</div><div class="line">74</div><div class="line">75</div><div class="line">76</div><div class="line">77</div><div class="line">78</div><div class="line">79</div><div class="line">80</div><div class="line">81</div><div class="line">82</div><div class="line">83</div><div class="line">84</div><div class="line">85</div><div class="line">86</div><div class="line">87</div><div class="line">88</div><div class="line">89</div><div class="line">90</div><div class="line">91</div><div class="line">92</div><div class="line">93</div><div class="line">94</div><div class="line">95</div><div class="line">96</div><div class="line">97</div><div class="line">98</div><div class="line">99</div><div class="line">100</div><div class="line">101</div></pre></td><td class="code"><pre><div class="line">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;</div><div class="line">&lt;project xmlns=&quot;http://maven.apache.org/POM/4.0.0&quot;</div><div class="line">         xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;</div><div class="line">         xsi:schemaLocation=&quot;http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd&quot;&gt;</div><div class="line">    &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;</div><div class="line"></div><div class="line">    &lt;groupId&gt;com.bjxl&lt;/groupId&gt;</div><div class="line">    &lt;artifactId&gt;spring4&lt;/artifactId&gt;</div><div class="line">    &lt;version&gt;1.0-SNAPSHOT&lt;/version&gt;</div><div class="line"></div><div class="line">    &lt;properties&gt;</div><div class="line">        &lt;kotlin.version&gt;1.1.51&lt;/kotlin.version&gt;</div><div class="line">    &lt;/properties&gt;</div><div class="line"></div><div class="line">    &lt;dependencies&gt;</div><div class="line">        &lt;dependency&gt;</div><div class="line">            &lt;groupId&gt;org.springframework&lt;/groupId&gt;</div><div class="line">            &lt;artifactId&gt;spring-core&lt;/artifactId&gt;</div><div class="line">            &lt;version&gt;4.2.6.RELEASE&lt;/version&gt;</div><div class="line">        &lt;/dependency&gt;</div><div class="line">        &lt;dependency&gt;</div><div class="line">            &lt;groupId&gt;org.springframework&lt;/groupId&gt;</div><div class="line">            &lt;artifactId&gt;spring-beans&lt;/artifactId&gt;</div><div class="line">            &lt;version&gt;4.2.6.RELEASE&lt;/version&gt;</div><div class="line">        &lt;/dependency&gt;</div><div class="line">        &lt;dependency&gt;</div><div class="line">            &lt;groupId&gt;org.springframework&lt;/groupId&gt;</div><div class="line">            &lt;artifactId&gt;spring-context&lt;/artifactId&gt;</div><div class="line">            &lt;version&gt;4.2.6.RELEASE&lt;/version&gt;</div><div class="line">        &lt;/dependency&gt;</div><div class="line">        &lt;dependency&gt;</div><div class="line">            &lt;groupId&gt;org.springframework&lt;/groupId&gt;</div><div class="line">            &lt;artifactId&gt;spring-expression&lt;/artifactId&gt;</div><div class="line">            &lt;version&gt;4.2.6.RELEASE&lt;/version&gt;</div><div class="line">        &lt;/dependency&gt;</div><div class="line">        &lt;!--单元测试--&gt;</div><div class="line">        &lt;dependency&gt;</div><div class="line">            &lt;groupId&gt;junit&lt;/groupId&gt;</div><div class="line">            &lt;artifactId&gt;junit&lt;/artifactId&gt;</div><div class="line">            &lt;version&gt;4.12&lt;/version&gt;</div><div class="line">        &lt;/dependency&gt;</div><div class="line">        &lt;!--日志--&gt;</div><div class="line">        &lt;dependency&gt;</div><div class="line">            &lt;groupId&gt;log4j&lt;/groupId&gt;</div><div class="line">            &lt;artifactId&gt;log4j&lt;/artifactId&gt;</div><div class="line">            &lt;version&gt;1.2.17&lt;/version&gt;</div><div class="line">        &lt;/dependency&gt;</div><div class="line">        &lt;!--相当于slf4j--&gt;</div><div class="line">        &lt;dependency&gt;</div><div class="line">            &lt;groupId&gt;commons-logging&lt;/groupId&gt;</div><div class="line">            &lt;artifactId&gt;commons-logging&lt;/artifactId&gt;</div><div class="line">            &lt;version&gt;1.2&lt;/version&gt;</div><div class="line">        &lt;/dependency&gt;</div><div class="line">        &lt;dependency&gt;</div><div class="line">            &lt;groupId&gt;org.jetbrains.kotlin&lt;/groupId&gt;</div><div class="line">            &lt;artifactId&gt;kotlin-stdlib-jre8&lt;/artifactId&gt;</div><div class="line">            &lt;version&gt;$&#123;kotlin.version&#125;&lt;/version&gt;</div><div class="line">        &lt;/dependency&gt;</div><div class="line">        &lt;dependency&gt;</div><div class="line">            &lt;groupId&gt;org.jetbrains.kotlin&lt;/groupId&gt;</div><div class="line">            &lt;artifactId&gt;kotlin-test&lt;/artifactId&gt;</div><div class="line">            &lt;version&gt;$&#123;kotlin.version&#125;&lt;/version&gt;</div><div class="line">            &lt;scope&gt;test&lt;/scope&gt;</div><div class="line">        &lt;/dependency&gt;</div><div class="line">    &lt;/dependencies&gt;</div><div class="line"></div><div class="line">    &lt;build&gt;</div><div class="line">        &lt;plugins&gt;</div><div class="line">            &lt;plugin&gt;</div><div class="line">                &lt;groupId&gt;org.jetbrains.kotlin&lt;/groupId&gt;</div><div class="line">                &lt;artifactId&gt;kotlin-maven-plugin&lt;/artifactId&gt;</div><div class="line">                &lt;version&gt;$&#123;kotlin.version&#125;&lt;/version&gt;</div><div class="line">                &lt;executions&gt;</div><div class="line">                    &lt;execution&gt;</div><div class="line">                        &lt;id&gt;compile&lt;/id&gt;</div><div class="line">                        &lt;phase&gt;compile&lt;/phase&gt;</div><div class="line">                        &lt;goals&gt;</div><div class="line">                            &lt;goal&gt;compile&lt;/goal&gt;</div><div class="line">                        &lt;/goals&gt;</div><div class="line">                        &lt;configuration&gt;</div><div class="line">                            &lt;sourceDirs&gt;</div><div class="line">                                &lt;source&gt;src/main/java&lt;/source&gt;</div><div class="line">                                &lt;source&gt;src/main/interface&lt;/source&gt;</div><div class="line">                            &lt;/sourceDirs&gt;</div><div class="line">                        &lt;/configuration&gt;</div><div class="line">                    &lt;/execution&gt;</div><div class="line">                    &lt;execution&gt;</div><div class="line">                        &lt;id&gt;test-compile&lt;/id&gt;</div><div class="line">                        &lt;phase&gt;test-compile&lt;/phase&gt;</div><div class="line">                        &lt;goals&gt;</div><div class="line">                            &lt;goal&gt;test-compile&lt;/goal&gt;</div><div class="line">                        &lt;/goals&gt;</div><div class="line">                    &lt;/execution&gt;</div><div class="line">                &lt;/executions&gt;</div><div class="line">                &lt;configuration&gt;</div><div class="line">                    &lt;jvmTarget&gt;1.8&lt;/jvmTarget&gt;</div><div class="line">                &lt;/configuration&gt;</div><div class="line">            &lt;/plugin&gt;</div><div class="line">        &lt;/plugins&gt;</div><div class="line">    &lt;/build&gt;</div><div class="line">&lt;/project&gt;</div></pre></td></tr></table></figure></p>
<p>ServiceImpl<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div></pre></td><td class="code"><pre><div class="line">package impl</div><div class="line"></div><div class="line">import face.ISomeService</div><div class="line"></div><div class="line">/**</div><div class="line"> * Created by futao on 2017/9/29.</div><div class="line"> */</div><div class="line">class ISomeServiceImpl : ISomeService &#123;</div><div class="line">    override fun doFirst(): String &#123;</div><div class="line">        println(&quot;执行doFirst()方法&quot;)</div><div class="line">        return &quot;abcde&quot;</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    override fun doSecond() &#123;</div><div class="line">        println(&quot;执行doSecond()方法&quot;)</div><div class="line">    &#125;</div><div class="line"></div><div class="line">&#125;</div></pre></td></tr></table></figure></p>
<p>applicationContext.xml<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div></pre></td><td class="code"><pre><div class="line">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;</div><div class="line">&lt;beans xmlns=&quot;http://www.springframework.org/schema/beans&quot;</div><div class="line">       xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;</div><div class="line">       xsi:schemaLocation=&quot;http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd&quot;&gt;</div><div class="line"></div><div class="line">    &lt;!--bean容器--&gt;</div><div class="line">    &lt;!--对象容器--&gt;</div><div class="line">    &lt;!--注册Service对象，等于Java代码中 ISomeServiceImpl iss = new ISomeServiceImpl()</div><div class="line">        这个对象是在Spring容器被初始化的时候Spring自动创建的</div><div class="line">    --&gt;</div><div class="line">    &lt;bean id=&quot;iss&quot; class=&quot;impl.ISomeServiceImpl&quot;/&gt;</div><div class="line">&lt;/beans&gt;</div></pre></td></tr></table></figure></p>
<p>测试<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div></pre></td><td class="code"><pre><div class="line">package ServiceTest</div><div class="line"></div><div class="line">import impl.ISomeServiceImpl</div><div class="line">import org.junit.Test</div><div class="line">import org.springframework.context.ApplicationContext</div><div class="line">import org.springframework.context.support.ClassPathXmlApplicationContext</div><div class="line"></div><div class="line">/**</div><div class="line"> * Created by futao on 2017/9/29.</div><div class="line"> */</div><div class="line">class myTest &#123;</div><div class="line">    /**</div><div class="line">     * 传统方式 new对象</div><div class="line">     */</div><div class="line">    @Test</div><div class="line">    fun test() &#123;</div><div class="line">        val iSomeServiceImpl = ISomeServiceImpl()</div><div class="line">        iSomeServiceImpl.doFirst()</div><div class="line">        iSomeServiceImpl.doSecond()</div><div class="line">    &#125;</div><div class="line"></div><div class="line">     /**</div><div class="line">     * 从bean容器里面获取</div><div class="line">     */</div><div class="line">    @Test</div><div class="line">    fun test02() &#123;</div><div class="line">        //加载Spring配置文件，创建Spring对象，这个时候对象就已经全部被加载到Spring容器当中了</div><div class="line">//        val container = ClassPathXmlApplicationContext(&quot;applicationContext.xml&quot;)</div><div class="line">        val container = FileSystemXmlApplicationContext(&quot;D:\\eclipse-workspace\\spring4\\src\\main\\resources\\applicationContext.xml&quot;)</div><div class="line">        //从容器中获取指定Bean对象</div><div class="line">        val iss = container.getBean(&quot;iss&quot;) as ISomeServiceImpl</div><div class="line">        iss.doSecond()</div><div class="line">        iss.doFirst()</div><div class="line">    &#125;</div><div class="line">&#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure></p>
<h2 id="BeanFactory容器"><a href="#BeanFactory容器" class="headerlink" title="BeanFactory容器"></a>BeanFactory容器</h2><p><strong>装配时机：</strong></p>
<ol>
<li>ApplicationContext容器，会在容器对象初始化时，将其中的所有对象一次性全部装配好。以后代码中若要使用到这些对象，只需要从内存中读取就行，执行效率高，但是占内存。</li>
<li>BeanFactory容器，对容器中对象的装配与加载采用延迟策略，即在第一次调用getBean()时，才真正装配（new）该对象。</li>
</ol>
<h2 id="bean的装配"><a href="#bean的装配" class="headerlink" title="bean的装配"></a>bean的装配</h2><p><strong>动态工厂bean</strong><br><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div></pre></td><td class="code"><pre><div class="line">package Factory</div><div class="line"></div><div class="line">import impl.ISomeServiceImpl</div><div class="line"></div><div class="line">/**</div><div class="line"> * Created by futao on 2017/9/29.</div><div class="line"> */</div><div class="line">class dynamicFactory &#123;</div><div class="line">    fun getSomeService(): ISomeServiceImpl &#123;</div><div class="line">        return ISomeServiceImpl()</div><div class="line">    &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div></pre></td><td class="code"><pre><div class="line">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;</div><div class="line">&lt;beans xmlns=&quot;http://www.springframework.org/schema/beans&quot;</div><div class="line">       xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;</div><div class="line">       xsi:schemaLocation=&quot;http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd&quot;&gt;</div><div class="line"></div><div class="line">    &lt;!--bean容器--&gt;</div><div class="line">    &lt;!--对象容器--&gt;</div><div class="line">    &lt;!--注册Service对象，等于Java代码中 ISomeServiceImpl iss = new ISomeServiceImpl()</div><div class="line">        这个对象是在Spring容器被初始化的时候Spring自动创建的</div><div class="line">    --&gt;</div><div class="line">    &lt;!--&lt;bean id=&quot;iss&quot; class=&quot;impl.ISomeServiceImpl&quot;/&gt;--&gt;</div><div class="line">    &lt;bean id=&quot;issFactory&quot; class=&quot;Factory.dynamicFactory&quot;/&gt;</div><div class="line">    &lt;bean id=&quot;iss&quot; factory-bean=&quot;issFactory&quot; factory-method=&quot;getSomeService&quot;/&gt;</div><div class="line">&lt;/beans&gt;</div></pre></td></tr></table></figure>
<p><strong>静态工厂bean</strong><br>….</p>
<h4 id="容器中Bean的作用域"><a href="#容器中Bean的作用域" class="headerlink" title="容器中Bean的作用域"></a>容器中Bean的作用域</h4><p><strong>scope=”prototype”</strong>   原型模式，每次都会创建一个新的对象（真正使用的时候才会被创建，每获取一次，都会创建一个新的不同的对象）<br><strong>scope=”singleton”</strong>    单例模式，对象只会被创建一次（容器初始化的时候就会进行创建，也就是xml文件加载的时候）（默认的）</p>
<p>对于scope为singleton的单例模式，该Bean在容器被创建的时候即被装配好了。<br>对于scope为prototype的原型模式，Bean实例是在代码中使用该Bean实例时才进行装配的。</p>
<h2 id="Bean后处理器"><a href="#Bean后处理器" class="headerlink" title="Bean后处理器"></a><a href="http://blog.csdn.net/kuailefangyuan/article/details/17791523" target="_blank" rel="external">Bean后处理器</a></h2><p>只要是实现BeanPostProcessor 接口的一个类就可以作为Bean后处理器<br>重写方法postProcessAfterInitialization()和postProcessAfterInitialization()</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div></pre></td><td class="code"><pre><div class="line">package beanHandler</div><div class="line"></div><div class="line">import org.springframework.beans.factory.config.BeanPostProcessor</div><div class="line"></div><div class="line">/**</div><div class="line"> * Created by futao on 2017/9/29.</div><div class="line"> */</div><div class="line">class myBeanPostPrecess : BeanPostProcessor &#123;</div><div class="line">    //p0 当前调用执行&apos;Bean后处理器&apos;的Bean对象</div><div class="line">    //p1 当前调用执行&apos;Bean后处理器&apos;的bean对象的id</div><div class="line">    /**</div><div class="line">     * Bean后处理器</div><div class="line">     * 主要负责对容器初始化其他Bean后进行进一步增强处理</div><div class="line">     * 当Spring容器实例化Bean实例之后，就偶会依次调用Bean后处理器的两个方法对实例Bean进行增强处理。</div><div class="line">     */</div><div class="line">    override fun postProcessBeforeInitialization(p0: Any?, p1: String?): Any &#123;</div><div class="line">        println(&quot;执行了$&#123;p0!!::class.java.simpleName&#125;，$p1,Before&quot;)</div><div class="line">        return p0</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    override fun postProcessAfterInitialization(p0: Any?, p1: String?): Any &#123;</div><div class="line">        println(&quot;执行了$&#123;p0!!::class.java.simpleName&#125;，$p1,After&quot;)</div><div class="line">        return p0</div><div class="line">    &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>在applicationContext.xml配置文件中进行注册<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">&lt;!--Bean后处理器，因为是其他Bean在创建的时候自动d调用该Bean的，所以Bean后处理器的Bean是不需要id的--&gt;</div><div class="line">&lt;bean class=&quot;beanHandler.myBeanPostPrecess&quot;/&gt;</div></pre></td></tr></table></figure></p>
<p>测试<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div></pre></td><td class="code"><pre><div class="line">@Test</div><div class="line">    fun test03() &#123;</div><div class="line">        val iss = ClassPathXmlApplicationContext(&quot;applicationContext.xml&quot;)</div><div class="line">        val service = iss.getBean(&quot;iss&quot;) as ISomeServiceImpl</div><div class="line">        service.doFirst()</div><div class="line">        service.doSecond()</div><div class="line"></div><div class="line"></div><div class="line">        println(&quot;================&quot;)</div><div class="line"></div><div class="line">        val s2=iss.getBean(&quot;dyb&quot;) as ISomeServiceImpl</div><div class="line">        s2.doSecond()</div><div class="line">        s2.doFirst()</div><div class="line">    &#125;</div></pre></td></tr></table></figure></p>
<p>结果</p>
<p><img src="http://upload-images.jianshu.io/upload_images/1846623-7444effb62467040.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="Result"></p>
<h4 id="Bean-初始化之后，销毁之前-的生命周期"><a href="#Bean-初始化之后，销毁之前-的生命周期" class="headerlink" title="Bean(初始化之后，销毁之前)的生命周期"></a>Bean(初始化之后，销毁之前)的生命周期</h4><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div></pre></td><td class="code"><pre><div class="line">package impl</div><div class="line"></div><div class="line">import face.ISomeService</div><div class="line"></div><div class="line">/**</div><div class="line"> * Created by futao on 2017/9/29.</div><div class="line"> */</div><div class="line">class ISomeServiceImpl : ISomeService &#123;</div><div class="line">    override fun doFirst(): String &#123;</div><div class="line">        println(&quot;执行doFirst()方法&quot;)</div><div class="line">        return &quot;abcde&quot;</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    override fun doSecond() &#123;</div><div class="line">        println(&quot;执行doSecond()方法&quot;)</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    fun initAtfer() &#123;</div><div class="line">        println(&quot;初始化之后&quot;)</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    fun beforeDestory() &#123;</div><div class="line">        println(&quot;销毁之前&quot;)</div><div class="line">    &#125;</div><div class="line"></div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>XML<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">&lt;bean id=&quot;iss&quot; class=&quot;impl.ISomeServiceImpl&quot; init-method=&quot;initAtfer&quot; destroy-method=&quot;beforeDestory&quot;/&gt;</div></pre></td></tr></table></figure></p>
<p>Test<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div></pre></td><td class="code"><pre><div class="line">@Test</div><div class="line">  fun test03() &#123;</div><div class="line">  </div><div class="line">      val iss = ClassPathXmlApplicationContext(&quot;applicationContext.xml&quot;)</div><div class="line">      val service = iss.getBean(&quot;iss&quot;) as ISomeServiceImpl</div><div class="line">      println(service.doFirst())</div><div class="line">      service.doSecond()</div><div class="line">      println(&quot;================&quot;)</div><div class="line">      val s2=iss.getBean(&quot;dyb&quot;) as ISomeServiceImpl</div><div class="line">      s2.doSecond()</div><div class="line">      s2.doFirst()</div><div class="line">  //销毁方法的执行需要两个要求</div><div class="line">      /*</div><div class="line">      *   1)被销毁的对象需要是singleton的</div><div class="line">      *   2)容器要显式地关闭</div><div class="line">      * */</div><div class="line">      iss.destroy()</div><div class="line">  &#125;</div></pre></td></tr></table></figure></p>
<h4 id="注册的时候id和name的区别"><a href="#注册的时候id和name的区别" class="headerlink" title="注册的时候id和name的区别"></a>注册<bean>的时候id和name的区别</bean></h4><p><strong>id:</strong> id的命名需要满足XMl对ID属性的命名规范：必须以字母开头，可以包含字母，数字，下划线，连字符，句号，冒号<br><strong>name:</strong>name属性值可以包含任何符号</p>

    </div>
    <p class="post-meta">
        <span class="post-cat">分类：
            <a class="cat-link" href="/coder957/categories/Java/">Java</a>|<a class="cat-link" href="/coder957/categories/Java/学习/">学习</a>
        </span>
        <span class="post-tags">
            标签：
            
    
        <a href="/coder957/tags/Java/" title="Java">Java</a> / 
    
        <a href="/coder957/tags/Spring/" title="Spring">Spring</a> / 
    
        <a href="/coder957/tags/Kotlin/" title="Kotlin">Kotlin</a>
    

        </span>
    </p>
</article>
<!-- 分享按钮 -->

  <div class="article-share clearfix text-center">
    <div class="share-area">
      <span class="share-txt">分享到：</span>
      <a href="javascript: window.open('http://service.weibo.com/share/share.php?url=' + encodeURIComponent(location.href) + '&title=' + document.title + '&language=zh_cn');" class="share-icon weibo"></a>
      <a href="javascript: alert('请复制链接到微信并发送');" class="share-icon wechat"></a>
      <a href="javascript: window.open('http://sns.qzone.qq.com/cgi-bin/qzshare/cgi_qzshare_onekey?url=' + encodeURIComponent(location.href) + '&title=' + document.title);" class="share-icon qqzone"></a>
      <a href="javascript: window.open('http://connect.qq.com/widget/shareqq/index.html?url=' + encodeURIComponent(location.href) + '&desc=Jelon个人博客&title=' + document.title + '&callback=' + encodeURIComponent(location.href));" class="share-icon qq"></a>
      <a href="javascript: window.open('http://shuo.douban.com/!service/share?href=' + encodeURIComponent(location.href) + '&name=' + document.title + '&text=' + document.title);" class="share-icon douban"></a>
    </div>
  </div>


    <!-- 上一篇/下一篇 -->

<div class="article-nav clearfix">
    
    <span class="prev fl">
        上一篇<br >
        <a href="/coder957/2017/10/09/Java中的一些名词/">
            
                Java中的一些名词
            
        </a>
    </span>
    

    
    <span class="next fr">
        下一篇<br >
        <a href="/coder957/2017/09/28/hibernate-annotation/">
            
                hibernate-annotation
            
        </a>
    </span>
    
</div>

        <!-- 文章评论 -->
        
  <script src="/coder957/js/comment.js"></script>
  <div id="comments" class="comment">
    <!--
    <div class="sign-bar">
      GitHub 已登录!
      <span class="sign-link">登出</span>
    </div>
    <section class="box">
      <div class="com-avatar"><img src="/img/jelon.jpg" alt="avatar"></div>
      <div class="com-text">
        <div class="main">
          <textarea class="text-area-edited show" placeholder="欢迎评论！"></textarea>
          <div class="text-area-preview"></div>
        </div>
        <div class="switch">
          <div class="switch-item on">编辑</div>
          <div class="switch-item">预览</div>
        </div>
        <div class="button">提交</div>
      </div>
    </section>
    <section class="tips">注：评论支持 markdown 语法！</section>
    <section class="list-wrap">
      <ul class="list">
        <li>
          <div class="user-avatar">
            <a href="/">
              <img src="/img/jelon.jpg" alt="user-avatar">
            </a>
          </div>
          <div class="user-comment">
            <div class="user-comment-header">
              <span class="post-name">张德龙</span>
              <span class="post-time">2017年12月12日</span>
              <span class="like liked">已赞</span>
              <span class="like-num">2</span>
            </div>
            <div class="user-comment-body">333333</div>
          </div>
        </li>
        <li>
          <div class="user-avatar">
            <a href="/">
              <img src="/img/jelon.jpg" alt="user-avatar">
            </a>
          </div>
          <div class="user-comment">
            <div class="user-comment-header">
              <span class="post-name">刘德华</span>
              <span class="post-time">2017年12月12日</span>
              <span class="like">点赞</span>
              <span class="like-num">2</span>
            </div>
            <div class="user-comment-body">vvvvv</div>
          </div>
        </li>
      </ul>
      <div class="page-nav">
        <a href="javascript: void(0);" class="item">1</a>
        <a href="javascript: void(0);" class="item">2</a>
        <a href="javascript: void(0);" class="item current">3</a>
      </div>
    </section>
    -->
  </div>
  <script>
  JELON.Comment({
    container: 'comments',
    label: 'Spring入门' || '2017/09/29/Spring入门/',
    owner: 'FutaoSmile',
    repo: 'blog_comments',
    clientId: '149ec394071cfd7f0f47',
    clientSecret: 'd1c988f53efa3f0f8651799f8212cc8e2cab24b9'
  });
  </script>


            </div>

        </section>
        <!-- 侧栏部分 -->
<aside class="sidebar">
    <section class="widget">
        <h3 class="widget-hd"><strong>文章分类</strong></h3>
        <!-- 文章分类 -->
<ul class="widget-bd">
    
    <li>
        <a href="/coder957/categories/后端/">后端</a>
        <span class="badge">(5)</span>
    </li>
    
    <li>
        <a href="/coder957/categories/Java/">Java</a>
        <span class="badge">(8)</span>
    </li>
    
    <li>
        <a href="/coder957/categories/Web技术/">Web技术</a>
        <span class="badge">(3)</span>
    </li>
    
    <li>
        <a href="/coder957/categories/后端/Java/">Java</a>
        <span class="badge">(5)</span>
    </li>
    
    <li>
        <a href="/coder957/categories/GIt/">GIt</a>
        <span class="badge">(1)</span>
    </li>
    
    <li>
        <a href="/coder957/categories/Java/Spring/">Spring</a>
        <span class="badge">(3)</span>
    </li>
    
    <li>
        <a href="/coder957/categories/Java/学习/">学习</a>
        <span class="badge">(5)</span>
    </li>
    
    <li>
        <a href="/coder957/categories/Web技术/Java/">Java</a>
        <span class="badge">(1)</span>
    </li>
    
    <li>
        <a href="/coder957/categories/Web技术/Kotlin/">Kotlin</a>
        <span class="badge">(1)</span>
    </li>
    
    <li>
        <a href="/coder957/categories/GIt/Web技术/">Web技术</a>
        <span class="badge">(1)</span>
    </li>
    
    <li>
        <a href="/coder957/categories/学习/">学习</a>
        <span class="badge">(1)</span>
    </li>
    
</ul>
    </section>

    
    <section class="widget">
        <h3 class="widget-hd"><strong>热门标签</strong></h3>
        <!-- 文章标签 -->
<div class="widget-bd tag-wrap">
  
    <a class="tag-item" href="/coder957/tags/Java/" title="Java">Java (12)</a>
  
    <a class="tag-item" href="/coder957/tags/Spring/" title="Spring">Spring (9)</a>
  
    <a class="tag-item" href="/coder957/tags/kotlin/" title="kotlin">kotlin (7)</a>
  
    <a class="tag-item" href="/coder957/tags/Maven/" title="Maven">Maven (2)</a>
  
    <a class="tag-item" href="/coder957/tags/Hibernate/" title="Hibernate">Hibernate (2)</a>
  
    <a class="tag-item" href="/coder957/tags/kotlin-小知识/" title="kotlin 小知识">kotlin 小知识 (1)</a>
  
    <a class="tag-item" href="/coder957/tags/Kotlin/" title="Kotlin">Kotlin (1)</a>
  
    <a class="tag-item" href="/coder957/tags/Json/" title="Json">Json (1)</a>
  
    <a class="tag-item" href="/coder957/tags/热点文章/" title="热点文章">热点文章 (1)</a>
  
    <a class="tag-item" href="/coder957/tags/博主推荐/" title="博主推荐">博主推荐 (1)</a>
  
    <a class="tag-item" href="/coder957/tags/转载/" title="转载">转载 (1)</a>
  
    <a class="tag-item" href="/coder957/tags/资料/" title="资料">资料 (1)</a>
  
    <a class="tag-item" href="/coder957/tags/Ajax/" title="Ajax">Ajax (1)</a>
  
</div>
    </section>
    

    

    
    <!-- 友情链接 -->
    <section class="widget">
        <h3 class="widget-hd"><strong>友情链接</strong></h3>
        <!-- 文章分类 -->
<ul class="widget-bd">
    
        <li>
            <a href="http://lsc95.github.io/" target="_blank" title="椿哥的狗窝">椿哥的狗窝</a>
        </li>
    
        <li>
            <a href="http://futaosmile.oschina.io/documents/" target="_blank" title="donot click me">Document</a>
        </li>
    
        <li>
            <a href="" target="_blank" title="虚位以待">loading...</a>
        </li>
    
</ul>
    </section>
    
</aside>
<!-- / 侧栏部分 -->
    </div>

    <!-- 博客底部 -->
    <footer class="footer">
    &copy;
    
                2016-
                2017
                    

                        <a href="/coder957/">Loves You</a>
</footer>
<div class="back-to-top" id="JELON__backToTop" title="返回顶部">返回顶部</div>
    <!--博客js脚本 -->
    <!-- 这里放网站js脚本 -->
<script src="/coder957/js/main.js"></script>
</body>
</html>